﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Globalization;
using System.Windows.Forms;
namespace VehicleScale
{
    public class LogFile
    {
        #region Variables
        internal static string dirLog = Application.StartupPath + @"\Log";
        internal static string filePath = Application.StartupPath + @"\Log\" ;
        internal static string nameFile;
        internal static string passwordFile = "HATIEN";

        internal static string userLogin;
        //internal static string thaotac;

        #endregion

        #region Method
        //Create a Folder
        public static bool CreateFolder(string strPath)
        {
            try
            {
                if (Directory.Exists(strPath) == false)
                {
                    Directory.CreateDirectory(strPath);
                   
                }

                return true;
            }
            catch
            {
                return false;
            }
        }

        //Create a File by date current

        public static void CreateFile(string nameFile)
        {

            if (!File.Exists(nameFile))
                File.Create(nameFile);
        }

        //Search File 
        public static bool IsExistFile(string filename)
        {
            List<string> _listFile = GetListFiles(filePath);
            if (_listFile.Count == 0)
            {
                return false;
            }
            else
            {

                if (_listFile.Equals(filename))
                {
                    return true;
                }
                else
                    return false;
            }
        }

        //Delete a Folder

        public static void DeleteFolder(string strPath)
        {
            try
            {
                if (Directory.Exists(strPath) == true)
                {
                    Directory.Delete(strPath);
                }
                else
                {
                    Library.Tools.MsgBox.Show("Thư mục " + strPath + " đã tồn tại.");
                }
            }
            catch
            {
            }
        }

        //Delete all File in Folder

        public static void DeleteAllFile(string folder)
        {
            try
            {
                DirectoryInfo dir =  new DirectoryInfo(folder);
                if (Directory.Exists(folder) == true)
                {
                    foreach (FileInfo file in dir.GetFiles())
                    {
                        file.Delete();
                    }
                }
                else
                {
                    Library.Tools.MsgBox.Show("Thư mục " + folder + " không tồn tại.");
                }
            }
            catch
            {
            }
        }

        public static void WriteLog(string namefile, string userLogin, string message)
        {
            //if (!File.Exists(namefile))
            //    File.Create(namefile);
            //FileStream s = new FileStream(nameFile,FileMode.Open;
            //StreamWriter s = new StreamWriter(nameFile);
            
            //File.Exists(nameFile);

            using (StreamWriter w = File.AppendText(namefile))
            //using(StreamWriter w = new StreamWriter(nameFile,true))
            {
                w.WriteLine(String.Format("{0:d} {0:HH:mm:ss} - {1}  - {2}", DateTime.Now, userLogin, message));
                w.Flush();
                w.Close();
            }
        }

        public static string ReadLog(string namefile)
        {
            string s = String.Empty;
            if (!File.Exists(namefile))
            {
                Library.Tools.MsgBox.Show("File " + namefile + " không tìm thấy","Error");
            }
            using (StreamReader r = File.OpenText(namefile))
            {
                 s = r.ReadToEnd();
                r.Close();
            }
            return s;
        }

        public static void DeleteFile(string namefile)
        {
            FileInfo _file = new FileInfo(namefile);
            if (_file ==null)
            {
                Library.Tools.MsgBox.Show("File " + namefile + " không tìm thấy", "Error");
            }
            try
            {
                _file.Delete();
                //File.Delete(namefile);
            }
            catch(Exception ex)
            {
                Library.Tools.MsgBox.Show("Lỗi xóa file. Chi tiết lỗi : " + ex.Message);
            }
            

        }

        //Lay danh sach File co trong thu muc Log
        public static List<string> GetListFiles(string strPath)
        {
            List<string> _list = new List<string>();
            DirectoryInfo dir = new DirectoryInfo(strPath);
            foreach (FileInfo file in dir.GetFiles())
            {
                _list.Add(file.Name);
            }
            return _list;
        }
        #endregion


    }
}
